﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;
using System.Transactions;


namespace BusinessLogic
{
    static class KhachHangTraLaiBanBuon
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;
        public static Entities.KhachHangTraLaiBanBuon[] SelectKhachHangTraLaiBanBuon(Entities.KhachHangTraLaiBanBuon hoadon)
        {
            Entities.KhachHangTraLaiBanBuon[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KhachHangTraLaiBanBuon.sp_Select_KhachHangTraLaiBanBuon, cn);
                cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.HanhDong, SqlDbType.NVarChar).Value = hoadon.HanhDong;
                cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.MaKhachHangTraLaiBanBuon, SqlDbType.VarChar).Value = hoadon.MaKhachHangTraLaiBanBuon;
              //  cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.MaKho, SqlDbType.VarChar).Value = hoadon.MaKho;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.KhachHangTraLaiBanBuon row = new Entities.KhachHangTraLaiBanBuon();
                    row.KhachHangTraLaiBanBuonID = int.Parse(dr[Common.Constants.KhachHangTraLaiBanBuon.KhachHangTraLaiBanBuonID].ToString());
                    row.MaKhachHangTraLaiBanBuon = dr[Common.Constants.KhachHangTraLaiBanBuon.MaKhachHangTraLaiBanBuon].ToString();
                    row.NgayNhap = DateTime.Parse(dr[Common.Constants.KhachHangTraLaiBanBuon.NgayNhap].ToString());
                    row.MaKhachHang = dr[Common.Constants.KhachHangTraLaiBanBuon.MaKhachHang].ToString();
                    row.TenKhachHang = dr[Common.Constants.KhachHangTraLaiBanBuon.TenKhachHang].ToString();
                    row.DuNo = dr[Common.Constants.KhachHangTraLaiBanBuon.DuNo].ToString();
                    row.NguoiTra = dr[Common.Constants.KhachHangTraLaiBanBuon.NguoiTra].ToString();
                    row.MaKho = dr[Common.Constants.KhachHangTraLaiBanBuon.MaKho].ToString();
                    row.TenKho = dr[Common.Constants.KhachHangTraLaiBanBuon.TenKho].ToString();
                    row.MaNhanVien = dr[Common.Constants.KhachHangTraLaiBanBuon.MaNhanVien].ToString();
                    row.TenNhanVien = dr[Common.Constants.KhachHangTraLaiBanBuon.TenNhanVien].ToString();
                    row.MaTienTe = dr[Common.Constants.KhachHangTraLaiBanBuon.MaTienTe].ToString();
                    row.TyGiaTienTe = dr[Common.Constants.KhachHangTraLaiBanBuon.TyGiaTienTe].ToString();
                    row.Thue = dr[Common.Constants.KhachHangTraLaiBanBuon.Thue].ToString();
                    row.TienBoiThuong = dr[Common.Constants.KhachHangTraLaiBanBuon.TienBoiThuong].ToString();
                    row.ThanhToanNgay = dr[Common.Constants.KhachHangTraLaiBanBuon.ThanhToanNgay].ToString();
                    row.ThanhToanSauKhiLapPhieu = dr[Common.Constants.KhachHangTraLaiBanBuon.ThanhToanSauKhiLapPhieu].ToString();
                    row.GhiChu = dr[Common.Constants.KhachHangTraLaiBanBuon.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.KhachHangTraLaiBanBuon.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.KhachHangTraLaiBanBuon[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.KhachHangTraLaiBanBuon)arr[i];
                    }
                }
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                System.Windows.Forms.MessageBox.Show(s);
                list = null;
            }
            return list;
        }

        public static int InsertUpdateKhachHangTraLaiBanBuon(Entities.KhachHangTraLaiBanBuon hoadon)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                //using (ts = new TransactionScope(TransactionScopeOption.Required))
                //{
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(Common.Constants.KhachHangTraLaiBanBuon.sp_InsertUpdate_KhachHangTraLaiBanBuon, cn);

                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.HanhDong, SqlDbType.NVarChar).Value = hoadon.HanhDong;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.KhachHangTraLaiBanBuonID, SqlDbType.Int).Value = hoadon.KhachHangTraLaiBanBuonID;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.MaKhachHangTraLaiBanBuon, SqlDbType.VarChar).Value = hoadon.MaKhachHangTraLaiBanBuon;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.NgayNhap, SqlDbType.DateTime).Value = hoadon.NgayNhap;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.MaKhachHang, SqlDbType.VarChar).Value = hoadon.MaKhachHang;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.TenKhachHang, SqlDbType.NVarChar).Value = hoadon.TenKhachHang;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.DuNo, SqlDbType.Float).Value = hoadon.DuNo;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.NguoiTra, SqlDbType.NVarChar).Value = hoadon.NguoiTra;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.MaKho, SqlDbType.VarChar).Value = hoadon.MaKho;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.TenKho, SqlDbType.NVarChar).Value = hoadon.TenKho;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.MaNhanVien, SqlDbType.VarChar).Value = hoadon.MaNhanVien;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.TenNhanVien, SqlDbType.NVarChar).Value = hoadon.TenNhanVien;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.MaTienTe, SqlDbType.VarChar).Value = hoadon.MaTienTe;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.TyGiaTienTe, SqlDbType.Float).Value = hoadon.TyGiaTienTe;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.Thue, SqlDbType.Float).Value = hoadon.Thue;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.TienBoiThuong, SqlDbType.Float).Value = hoadon.TienBoiThuong;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.ThanhToanNgay, SqlDbType.Float).Value = hoadon.ThanhToanNgay;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.ThanhToanSauKhiLapPhieu, SqlDbType.Float).Value = hoadon.ThanhToanSauKhiLapPhieu;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.GhiChu, SqlDbType.NVarChar).Value = hoadon.GhiChu;
                    cmd.Parameters.Add(Common.Constants.KhachHangTraLaiBanBuon.Deleted, SqlDbType.Bit).Value = hoadon.Deleted;
                    rt = cmd.ExecuteNonQuery();
                    if (ChiTietKhachHangTraLaiBanBuon.InsertUpdateChiTietKhachHangTraLaiBanBuon(hoadon.DanhSach) == 0)
                    {
                       // ts.Dispose();
                        return 0;
                    }
                   
                   // ts.Complete();
               // }
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
               
            }
            return rt;
        }
    }
}
#endregion
